﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.Layout;
using System.Windows.Forms;
using System.Drawing;

namespace TR0217.ControlEx
{
    public class CenterLayoutEngine : LayoutEngine
    {
        Control _parent = null;
        Size _parentSize;
        int _offsetX = 0;
        LayoutEngine _initialLayoutEngine;
        bool _initialLayout = true;

        public CenterLayoutEngine(Control parent,LayoutEngine initialLayoutEngine)
        {
            _initialLayoutEngine = initialLayoutEngine;
            _parent = parent;
            _parentSize = parent.Size;
            _parent.SizeChanged += new EventHandler(_parent_SizeChanged);
        }

        void _parent_SizeChanged(object sender, EventArgs e)
        {
            _parentSize = _parent.Size;
        }

        public override void InitLayout(object child, BoundsSpecified specified)
        {
            base.InitLayout(child, specified);
            _initialLayout = false;
        }

        public override bool Layout(object container, System.Windows.Forms.LayoutEventArgs layoutEventArgs)
        {
            Control parent = container as Control;
            _offsetX = (_parent.Size.Width - _parentSize.Width) / 2;
            if (!_initialLayout)
            {
                Rectangle parentDisplayRectangle = parent.DisplayRectangle;
                foreach (Control c in parent.Controls)
                {
                    if (!c.Visible)
                    {
                        continue;
                    }
                    if (c.Dock == DockStyle.None)
                    {
                        int x = c.Location.X + _offsetX;
                        c.Location = new Point(x, c.Location.Y);
                    }
                    else if(c.Dock == DockStyle.Left || c.Dock == DockStyle.Right)
                    {
                        c.Height = parentDisplayRectangle.Height;
                        if (c.Dock == DockStyle.Right)
                        {
                            c.Left = parentDisplayRectangle.Right - c.Width;
                        }
                    }
                    else if (c.Dock == DockStyle.Top || c.Dock == DockStyle.Bottom)
                    {
                        c.Width = parentDisplayRectangle.Width;
                        if (c.Dock == DockStyle.Bottom )
                        {
                            c.Top = parentDisplayRectangle.Bottom - c.Height;
                        }
                    }
                    else
                    {
                        c.Height = parentDisplayRectangle.Height;
                        c.Width = parentDisplayRectangle.Width;
                    }

                }

                return false;
            }
            else
            {
                _initialLayoutEngine.Layout(parent, layoutEventArgs);
                _initialLayout = false;
                return true;
            }           
        }
    }
}
